package com.fcl.oschina.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.fcl.mylibrary.recyclerviewhelper.ScrollListener;
import com.fcl.mylibrary.reflect.ViewInject;
import com.fcl.mylibrary.ui.BaseFragment;
import com.fcl.mylibrary.ui.layout.SceneLayout;
import com.fcl.mylibrary.ui.layout.StickyLayout;
import com.fcl.oschina.R;
import com.fcl.oschina.entity.NewItem;
import com.fcl.oschina.ui.activity.BlogDetailActivity;
import com.fcl.oschina.ui.activity.NewsDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */

public class BaseRecyclerFragment<T> extends BaseFragment {

    @ViewInject(R.id.sticky_layout)
    protected StickyLayout mStickyLayout;

    @ViewInject(R.id.scene_layout)
    protected SceneLayout mSceneLayout;

    @ViewInject(R.id.recycler)
    protected RecyclerView mRecyclerView;

    protected List<T> mDatas = new ArrayList<>();
    protected RecyclerView.Adapter mAdapter;

    @Override
    protected void init() {
        super.init();
        mAdapter = getAdapter();
        mRecyclerView.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addOnScrollListener(new ScrollListener(linearLayoutManager) {
            @Override
            protected void onLoadMore(int curPage) {
                super.onLoadMore(curPage);
                loadMore(curPage);
            }
        });
        mStickyLayout.setRefreshListener(new StickyLayout.RefreshListener() {
            @Override
            public void refresh() {
                onRefresh();
            }
        });
        mSceneLayout.setLoading();
        initData();
    }

    protected void initData() {

    }

    protected void onRefresh() {

    }

    protected void loadMore(int curPage) {

    }

    protected RecyclerView.Adapter getAdapter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_recycler;
    }

    public void jumpActivity(int type, int id) {
        Intent intent;
        switch (type) {
            case 3://博客
                intent = new Intent(getActivity(), BlogDetailActivity.class);
                intent.putExtra("id", id);
                getActivity().startActivity(intent);
                break;
            default://资讯
                intent = new Intent(getActivity(), NewsDetailActivity.class);
                intent.putExtra("id", id);
                getActivity().startActivity(intent);
                break;
        }
    }
}
